﻿using ArtMath.Attribute;
using ArtMath.Component.CoordinateSystem;
using ArtMath.Component.Point.Interface;
using ArtMath.Core.Data;
using ArtMath.Data;
using ArtMath.PointCollection;
using ArtMath.Resource;
using ArtMath.Setting;
using System;
using System.ComponentModel;
using System.Drawing;
using ArtMath.CommonType;

namespace ArtMath.Component.Point
{
    /// <summary>
    /// 自由点（适用于极坐标系）
    /// </summary>
    [Serializable]
    public class PPoint : PPointBase, IFreePoint
    {
        public PPoint(SketchPad pad, string name, bool addToList = false) : base(pad, name, ToolTypeName.zyd, addToList)
        {
            OutlineColor = Settings.Instance.PointOutlineColor;
            FillColor = Settings.Instance.PointFillColor;
        }
        [Browsable(false)]
        public bool UserMoveLock { get; set; } = false;
        public override bool Movable
        {
            get
            {
                return ObjectState != ObjectState.Normal || !TempMoveLock && !UserMoveLock;
            }
        }

        public override bool Locked
        {
            get { return UserMoveLock; }
            set
            {
                UserMoveLock = value;
                base.Locked = value;
            }
        }
        public override void Paint(Graphics gra)
        {
            if (ObjectState == ObjectState.Normal && Pxy.IsPaintable && Visible)
            {
                RectangleF rect = new RectangleF((float)(Pxy.X - Size.Value - OutlineWidth.Value), (float)(Pxy.Y - Size.Value - OutlineWidth.Value), (float)((Size.Value + OutlineWidth.Value) * 2), (float)((Size.Value + OutlineWidth.Value) * 2));
                if (UserMoveLock)
                {
                    gra.FillRectangle(new SolidBrush(Color.FromArgb(127, Color.Orange)), rect);
                }
                base.Paint(gra);
                if (UserMoveLock)
                {
                    gra.DrawRectangles(new Pen(Color.Orange, 1), new RectangleF[] { rect });
                }
            }
        }
        public override PointCollectionsManager GetMoveSpace(Base sender = null)
        {
            if (UserMoveLock)
            {
                return new PointCollectionsManager(new DiscontinuityPoint(NewXY));
            }
            PointCollectionsManager mgr = base.GetMoveSpace(sender);
            if (OutHosts != null)
            {
                foreach (LineBase item in OutHosts)
                {
                    mgr = mgr.Intersect(item.NewSkeleton);
                    if (mgr.IsEmptySet)
                    {
                        mgr.AddPointCollection(new DiscontinuityPoint(NewXY));
                        break;
                    }
                }
            }
            return mgr;
        }
        public override void SetFromString(string exp)
        {
            string[] tokens = exp.Split(',', '，');
            if (tokens.Length == 2)
            {
                ρ.SetExpression(tokens[0].Trim());
                θ.SetExpression(tokens[1].Trim());
            }
            else
            {
                throw new ArgumentException(string.Format(Resources.Translate("AcceptNArgs"), Resources.JoinLocalizedString("Point", "Coordinate"), 2));
            }
        }
        public override void ForceMove(PointD location, bool raiseComponentChangedEvent = true)
        {
            base.ForceMove(location, raiseComponentChangedEvent);
            PolarCoordinate pc = Pad.Coordinate as PolarCoordinate;
            if (pc != null)
            {
                PointD tmp = pc.PxytoRT(Pxy);
                ρ.SetExpression(tmp.X, false);
                θ.SetExpression(tmp.Y, false);
            }
        }
        public override void CreateComponents()
        {
            ForceMove(SpareCoordinate_, false);
            base.CreateComponents();
        }
    }
}
